package com.ruoyi.mushcoin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.mushcoin.domain.TgUserTaskManage;
import com.ruoyi.mushcoin.domain.bo.TgUserTaskManageBo;
import com.ruoyi.mushcoin.domain.vo.TgUserTaskManageVo;
import com.ruoyi.mushcoin.mapper.TgUserTaskManageMapper;
import com.ruoyi.mushcoin.service.ITgUserTaskManageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.ruoyi.mushcoin.domain.table.TgUserTaskManageTableDef.TG_USER_TASK_MANAGE;

/**
 * 用户任务管理(H5)Service业务层处理
 *
 * @author billy
 * 2024-09-04
 */
@Service
public class TgUserTaskManageServiceImpl extends BaseServiceImpl<TgUserTaskManageMapper, TgUserTaskManage> implements ITgUserTaskManageService {
    @Resource
    private TgUserTaskManageMapper tgUserTaskManageMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_USER_TASK_MANAGE);
    }

    private QueryWrapper buildQueryWrapper(TgUserTaskManageBo tgUserTaskManageBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_USER_TASK_MANAGE.TASK_NAME.like(tgUserTaskManageBo.getTaskName()));
        queryWrapper.and(TG_USER_TASK_MANAGE.TASK_REWARDS.eq(tgUserTaskManageBo.getTaskRewards()));
        queryWrapper.and(TG_USER_TASK_MANAGE.DISABLE_STATUS.eq(tgUserTaskManageBo.getDisableStatus()));
        queryWrapper.and(TG_USER_TASK_MANAGE.TASK_GROUP.eq(tgUserTaskManageBo.getTaskGroup()));

        return queryWrapper;
    }

    /**
     * 查询用户任务管理(H5)
     *
     * @param id 用户任务管理(H5)主键
     * @return 用户任务管理(H5)
     */
    @Override
    public TgUserTaskManageVo selectById(Long id) {
        return this.getOneAs(query().where(TG_USER_TASK_MANAGE.ID.eq(id)), TgUserTaskManageVo.class);

    }

    /**
     * 查询用户任务管理(H5)列表
     *
     * @param tgUserTaskManageBo 用户任务管理(H5)Bo
     * @return 用户任务管理(H5)集合
     */
    @Override
    public List<TgUserTaskManageVo> selectList(TgUserTaskManageBo tgUserTaskManageBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserTaskManageBo);
        return this.listAs(queryWrapper, TgUserTaskManageVo.class);
    }

    /**
     * 分页查询用户任务管理(H5)列表
     *
     * @param tgUserTaskManageBo 用户任务管理(H5)Bo
     * @return 分页用户任务管理(H5)集合
     */
    @Override
    public TableDataInfo<TgUserTaskManageVo> selectPage(TgUserTaskManageBo tgUserTaskManageBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserTaskManageBo);
        Page<TgUserTaskManageVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgUserTaskManageVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增用户任务管理(H5)
     *
     * @param tgUserTaskManageBo 用户任务管理(H5)Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgUserTaskManageBo tgUserTaskManageBo) {
        TgUserTaskManage tgUserTaskManage = MapstructUtils.convert(tgUserTaskManageBo, TgUserTaskManage.class);
        boolean result=this.save(tgUserTaskManage);
        tgUserTaskManageBo.setId(tgUserTaskManage.getId());
        return result;
    }

    /**
     * 新增用户任务管理(H5)，前台提供主键值，一般用于导入的场合
     *
     * @param tgUserTaskManageBo 用户任务管理(H5)Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgUserTaskManageBo tgUserTaskManageBo) {
        TgUserTaskManage tgUserTaskManage = MapstructUtils.convert(tgUserTaskManageBo, TgUserTaskManage.class);


        return tgUserTaskManageMapper.insertWithPk(tgUserTaskManage) > 0;//前台传来主键值
    }

    /**
     * 修改用户任务管理(H5)
     *
     * @param tgUserTaskManageBo 用户任务管理(H5)Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgUserTaskManageBo tgUserTaskManageBo) {
        TgUserTaskManage tgUserTaskManage = MapstructUtils.convert(tgUserTaskManageBo, TgUserTaskManage.class);
        if (ObjectUtil.isNotNull(tgUserTaskManage) && ObjectUtil.isNotNull(tgUserTaskManage.getId())) {
            boolean updated = this.updateById(tgUserTaskManage);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除用户任务管理(H5)
     *
     * @param ids 需要删除的用户任务管理(H5)主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
